package com.ptu.servlet;

import com.google.gson.Gson;
import com.ptu.entity.Dept;
import com.ptu.service.DeptService;
import com.ptu.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 */
@WebServlet("/ptu/DeptServlet")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptServiceImpl();

    // 使用ajax查询部门信息
    private void deptFindAll1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Dept> depts = deptService.findAll();

        String deptJson = new Gson().toJson(depts);

        resp.getWriter().print(deptJson);
    }

    // 删除指定部门信息
    private void deptRemove(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Dept dept =createDept(req,resp);
        int del =deptService.removeDept(dept);
        if (del > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/DeptServlet?method=deptFindAll");
        } else {
            req.setAttribute("msg", "删除失败");
            req.getRequestDispatcher("/deptList.jsp").forward(req, resp);
        }
    }


    // 修改指定部门信息
    private void deptChange(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        // 1. 接受页面的数据
        Dept dept = createDept(req, resp);

        // 2. 数据的处理
        int change = deptService.changeDept(dept);

        // 3. 给用户做出响应
        if (change > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/DeptServlet?method=deptFindAll");
        } else {
            req.setAttribute("msg", "修改失败");
            req.getRequestDispatcher("/deptUpdate.jsp").forward(req, resp);
        }
    }

    // 查询单个部门信息
    private void deptFindOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer deptno = toInteger(req.getParameter("deptno"));

        Dept dep =deptService.findOneDept(deptno);

        if (dep != null) {
            req.setAttribute("dep", dep);
            req.getRequestDispatcher("/deptUpdate.jsp").forward(req, resp);
        }
    }

    // 查询部门信息
    private void deptFindAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 查询所有部门信息
        List<Dept> deptList = deptService.findAll();

        req.setAttribute("deptList", deptList);

        req.getRequestDispatcher("/deptList.jsp").forward(req, resp);
    }

    // 添加部门操作
    private void deptSave(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        // 1. 接受页面的.数据
        Dept dept = createDept(req, resp);

        // 2. 数据的处理
        int save = deptService.addDept(dept);

        // 3. 给用户做出响应
        if (save > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/DeptServlet?method=deptFindAll");
        } else {
            req.setAttribute("msg", "添加失败");
            req.getRequestDispatcher("/deptAdd.html").forward(req, resp);
        }
    }

    // 创建 dept 对象
    private Dept createDept(HttpServletRequest req, HttpServletResponse resp){

        Integer deptno = toInteger(req.getParameter("deptno"));  //部门编号

        String deptname = req.getParameter("deptname");    // 部门名称

        String location = req.getParameter("location");    // 部门位置

        Dept dept = new Dept();

        dept.setDeptno(deptno);

        dept.setDeptname(deptname);

        dept.setLocation(location);

        return dept;

    }
    private Integer toInteger(String value) {
        return value == null ? null : Integer.parseInt(value);
    }
}
